<!-- commom html -->
<textarea id="upload-editor-view" data-url="{{:MyUrl('admin/ueditor/index', ['path_type'=>empty($editor_path_type) ? 'common' : $editor_path_type])}}" style="display: none;"></textarea>

<!-- 公共底部钩子 -->
{{if !empty($plugins_admin_view_common_bottom_data) and is_array($plugins_admin_view_common_bottom_data) and (!isset($is_footer) or $is_footer eq 1)}}
    {{foreach $plugins_admin_view_common_bottom_data as $hook}}
        {{if is_string($hook) or is_int($hook)}}
            {{$hook|raw}}
        {{/if}}
    {{/foreach}}
{{/if}}

</body>
</html>

<!-- 类库 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/jquery/jquery-2.1.0.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/assets/js/amazeui.min.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- echarts 图表 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/echarts/echarts.min.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/echarts/macarons.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- ueditor 编辑器 -->
<script type='text/javascript' src="{{$my_public_url}}static/common/lib/ueditor/ueditor.config.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/ueditor/ueditor.all.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/ueditor/lang/zh-cn/zh-cn.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 颜色选择器 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/colorpicker/jquery.colorpicker.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 元素拖拽排序插件 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/dragsort/jquery.dragsort-0.5.2.min.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 动画数数 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/animation-count-to/animation.count.to.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- amazeui插件 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/amazeui-switch/amazeui.switch.min.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/amazeui-chosen/amazeui.chosen.min.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/amazeui-dialog/amazeui.dialog.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/common/lib/amazeui-tagsinput/amazeui.tagsinput.min.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 日期组件 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/My97DatePicker/WdatePicker.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 元素拖动 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/tdrag/tdrag.min.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 复制插件 -->
<script type='text/javascript' src="{{$public_host}}static/common/lib/clipboard/clipboard.min.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- baidu map -->
<!--{{if isset($is_load_baidu_map_api) and $is_load_baidu_map_api eq 1}}-->
    <!--<script type="text/javascript" src="{{$Think.__MY_HTTP__}}://api.map.baidu.com/api?v=2.0&ak={{:MyC('common_baidu_map_ak')}}"></script>-->
<!--{{/if}}-->

<!-- 隐藏编辑器初始化 -->
<script type="text/javascript">
    var upload_editor = UE.getEditor("upload-editor-view", {
        isShow: false,
        focus: false,
        enableAutoSave: false,
        autoSyncData: false,
        autoFloatEnabled:false,
        wordCount: false,
        sourceEditor: null,
        scaleEnabled:true,
        toolbars: [["insertimage", "insertvideo", "attachment"]]
    });
</script>

<!-- 项目公共 -->
<script type='text/javascript' src="{{$public_host}}static/common/js/common.js?v={{:MyC('home_static_cache_version')}}"></script>
<script type='text/javascript' src="{{$public_host}}static/admin/{{$default_theme}}/js/common.js?v={{:MyC('home_static_cache_version')}}"></script>

<!-- 应用插件公共js -->
{{if !empty($plugins_js)}}
    <script type='text/javascript' src="{{$public_host}}static/{{$plugins_js}}?v={{:MyC('home_static_cache_version')}}"></script>
{{/if}}

<!-- 当前控制器js -->
{{if !empty($module_js)}}
    <script type='text/javascript' src="{{$public_host}}static/{{$module_js}}?v={{:MyC('home_static_cache_version')}}"></script>
{{/if}}

<!-- js钩子 -->
{{if !empty($plugins_admin_js_data) and is_array($plugins_admin_js_data)}}
    {{foreach $plugins_admin_js_data as $hook}}
        {{if !empty($hook) and is_string($hook)}}
            {{if substr($hook, 0, 4) eq 'http'}}
                <script type='text/javascript' src="{{$hook}}?v={{:MyC('home_static_cache_version')}}"></script>
            {{else /}}
                <script type='text/javascript' src="{{$public_host}}{{$hook}}?v={{:MyC('home_static_cache_version')}}"></script>
            {{/if}}
        {{elseif is_array($hook) /}}
            {{foreach $hook as $hook_js}}
                {{if !empty($hook_js) and is_string($hook_js)}}
                    {{if substr($hook_js, 0, 4) eq 'http'}}
                        <script type='text/javascript' src="{{$hook_js}}?v={{:MyC('home_static_cache_version')}}"></script>
                    {{else /}}
                        <script type='text/javascript' src="{{$public_host}}{{$hook_js}}?v={{:MyC('home_static_cache_version')}}"></script>
                    {{/if}}
                {{/if}}
            {{/foreach}}
        {{/if}}
    {{/foreach}}
{{/if}}

<!-- 公共页面底部钩子 -->
{{if !empty($plugins_admin_common_page_bottom_data) and is_array($plugins_admin_common_page_bottom_data)}}
    {{foreach $plugins_admin_common_page_bottom_data as $hook}}
        {{if is_string($hook) or is_int($hook)}}
            {{$hook|raw}}
        {{/if}}
    {{/foreach}}
{{/if}}


<script type="text/javascript">
    function transformLanguage(newLanguage) {
        // 获取所有dom元素中文
        var transformStr = '';
        var temp_index = 0;
        // 获取所有dom元素
        function getChildDom(dom, type, data = {}) {
            if(type == 'read') {
                [...dom.children].forEach(v => {
                    // 判断中文
                    // /^[\u0391-\uFFE5]+$/
                    var re= /[\u4e00-\u9fa5]/g;
                    // 防止某些标签有内容并且有标签 ，或者有空格
                    var vHtml = $(v).contents().filter(function (index, content) {return content.nodeType === 3}).text().trim();
                    // 跳过script标签
                    if (re.test(vHtml) && v.tagName != 'SCRIPT') {
                        // 使用\n换行拼接、接口将返回数组
                        transformStr += `${vHtml}\n`
                    }
                    // 递归获取元素
                    getChildDom(v, type, data);
                })
            }else {
                [...dom.children].forEach(v => {
                    // 判断中文
                    // /^[\u0391-\uFFE5]+$/
                    var re= /[\u4e00-\u9fa5]/g;
                    var vHtml = $(v).contents().filter(function (index, content) {return content.nodeType === 3}).text().trim();
                    // 跳过script标签
                    if (re.test(vHtml) && v.tagName != 'SCRIPT') {
                        if((data[temp_index] || null) != null)
                        {
                            var transOld = data[temp_index]['src'];
                            var transNew = data[temp_index]['dst'];
                            // 防止标签里面还有标签，所以只替换里面的html,使用replace
                            $(v).html(
                                $(v).html().replace( transOld, transNew)
                            )
                            temp_index++;
                        }
                    }
                    // 递归获取元素
                    getChildDom(v, type, data);
                })
            }
        }
        getChildDom(document,'read');
        if((transformStr || null) != null)
        {
            getTranslateData();
            // 获取翻译
            // 接口申请地址 https://api.fanyi.baidu.com/
            function getTranslateData() {
                var appid = '20210425000799675';   // 百度翻译API的appid
                var key = 'VNlqhTcdY1QU09HfHN5L';   // 百度翻译API的key
                var salt = (new Date).getTime();
                var query = transformStr;
                var from = 'zh';
                var to = newLanguage;
                if(newLanguage=="zh"){
                    to = newLanguage;
                    from = 'spa';
                }
                var str1 = appid + query + salt + key;
                var sign = md5(str1);
                $.ajax({
                    url: '/api/index/tranlange',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        q: query,
                        appid: appid,
                        salt: salt,
                        from: from,
                        to: to,
                        sign: sign
                    },
                    success: function(data) {
                        data.trans_result && getChildDom(document, 'write', data.trans_result);
                    }
                });
            }
        }
    }
    // 页面加载完成再执行
    window.onload = function () {

        if(localStorage.getItem("language")==undefined||localStorage.getItem("language")==null){
            transformLanguage('spa');
            localStorage.setItem("language","spa");
        }else if (localStorage.getItem("language")=="spa"){
            transformLanguage('spa');
            localStorage.setItem("language","spa");
        }
        // transformLanguage1('en');
        // $("#china").click(function(){
        //     console.log(1);
        //     transformLanguage('zh');
        // })
        // $("#english").click(function(){
        //     console.log(2);
        //     transformLanguage('en');
        // })
    };
</script>
